package com.aaa.marketresearch.android.service;

import android.app.ProgressDialog;
import android.content.Context;
import com.aaa.marketresearch.android.data.ServerInfo;
import com.aaa.marketresearch.android.data.UserInfo;
import com.aaa.marketresearch.android.data.domain.FamilyMember;
import com.aaa.marketresearch.android.data.domain.Market;
import com.aaa.marketresearch.android.data.domain.Offer;
import com.aaa.marketresearch.android.util.connector.HttpTextDataRequest;
import com.aaa.marketresearch.android.util.connector.IDataTextResponse;
import org.json.simple.JSONObject;

/**
 * @author Nail
 */
public class HttpClient {

    private Context context;
    private ProgressDialog dialog;

    public HttpClient(Context context) {
        this.context = context;
    }

    public void login(IDataTextResponse iDataTextResponse) {
        final boolean dialogFlag;
        if (this.dialog == null) {
            dialogFlag = true;
        } else if (!this.dialog.isShowing()) {
            dialogFlag = true;
        } else {
            dialogFlag = false;
        }

        if (dialogFlag) {
            this.dialog = ProgressDialog.show(this.context, "", "Авторизация. Пожалуйста подождите...", true);
        }
        String serverUrl = ServerInfo.getInstance().getServerUrl();
        HttpTextDataRequest request = new HttpTextDataRequest(this.context);

        request.addResponseListener(new IDataTextResponse() {
            @Override
            public void onTextResponse(String text) {
                if (dialogFlag) {
                    dialog.hide();
                }
                UserInfo.getInstance().setJSONData(text);
            }

            @Override
            public void onError() {
                if (dialogFlag) {
                    dialog.hide();
                }
            }
        });

        request.addResponseListener(iDataTextResponse);

        request.setURL(serverUrl + "/mobile/login");
        request.addData("name", UserInfo.getInstance().getAccount());
        request.addData("pass", UserInfo.getInstance().getPassword());
        request.send(this.context);
    }

    public void loadProfessions(IDataTextResponse iDataTextResponse) {
        String serverUrl = ServerInfo.getInstance().getServerUrl();
        HttpTextDataRequest request = new HttpTextDataRequest(this.context);
        request.addResponseListener(iDataTextResponse);
        request.setURL(serverUrl + "/mobile/getProfessions");
        request.send(this.context);
    }

    public void getFamilyMembers(IDataTextResponse iDataTextResponse) {
        final boolean dialogFlag = (this.dialog != null && !this.dialog.isShowing());
        if (dialogFlag) {
            this.dialog = ProgressDialog.show(this.context, "", "Загрузка. Пожалуйста подождите...", true);
        }
        String serverUrl = ServerInfo.getInstance().getServerUrl();
        HttpTextDataRequest request = new HttpTextDataRequest(this.context);

        request.addResponseListener(new IDataTextResponse() {
            @Override
            public void onTextResponse(String text) {
                if (dialogFlag) {
                    dialog.hide();
                }
            }

            @Override
            public void onError() {
                if (dialogFlag) {
                    dialog.hide();
                }
            }
        });

        request.addResponseListener(iDataTextResponse);

        request.setURL(serverUrl + "/mobile/getFamilyMembers");
        request.addData("familyId", UserInfo.getInstance().getId().toString());
        request.send(this.context);
    }

    public void saveFamilyMember(FamilyMember familyMember, IDataTextResponse iDataTextResponse) {
        final boolean dialogFlag = (this.dialog != null && !this.dialog.isShowing());
        if (dialogFlag) {
            this.dialog = ProgressDialog.show(this.context, "", "Сохранение. Пожалуйста подождите...", true);
        }
        String serverUrl = ServerInfo.getInstance().getServerUrl();
        HttpTextDataRequest request = new HttpTextDataRequest(this.context);

        request.addResponseListener(new IDataTextResponse() {
            @Override
            public void onTextResponse(String text) {
                if (dialogFlag) {
                    dialog.hide();
                }
            }

            @Override
            public void onError() {
                if (dialogFlag) {
                    dialog.hide();
                }
            }
        });

        request.addResponseListener(iDataTextResponse);

        request.setURL(serverUrl + "/mobile/saveFamilyMember");
        request.addData("data", familyMember.printJSON());
        request.send(this.context);
    }

    public void getMarkets(IDataTextResponse iDataTextResponse) {
        final boolean dialogFlag = (this.dialog != null && !this.dialog.isShowing());
        if (dialogFlag) {
            this.dialog = ProgressDialog.show(this.context, "", "Загрузка. Пожалуйста подождите...", true);
        }
        String serverUrl = ServerInfo.getInstance().getServerUrl();
        HttpTextDataRequest request = new HttpTextDataRequest(this.context);

        request.addResponseListener(new IDataTextResponse() {
            @Override
            public void onTextResponse(String text) {
                if (dialogFlag) {
                    dialog.hide();
                }
            }

            @Override
            public void onError() {
                if (dialogFlag) {
                    dialog.hide();
                }
            }
        });

        request.addResponseListener(iDataTextResponse);

        request.setURL(serverUrl + "/mobile/getMarkets");
        request.send(this.context);
    }

    public void saveMarket(Market market, IDataTextResponse iDataTextResponse) {
        final boolean dialogFlag = (this.dialog != null && !this.dialog.isShowing());
        if (dialogFlag) {
            this.dialog = ProgressDialog.show(this.context, "", "Сохранение. Пожалуйста подождите...", true);
        }
        String serverUrl = ServerInfo.getInstance().getServerUrl();
        HttpTextDataRequest request = new HttpTextDataRequest(this.context);

        request.addResponseListener(new IDataTextResponse() {
            @Override
            public void onTextResponse(String text) {
                if (dialogFlag) {
                    dialog.hide();
                }
            }

            @Override
            public void onError() {
                if (dialogFlag) {
                    dialog.hide();
                }
            }
        });

        request.addResponseListener(iDataTextResponse);

        request.setURL(serverUrl + "/mobile/saveMarket");
        request.addData("data", market.printJSON());
        request.send(this.context);
    }

    public void saveBill(JSONObject jBill, IDataTextResponse iDataTextResponse) {
        final boolean dialogFlag = (this.dialog != null && !this.dialog.isShowing());
        if (dialogFlag) {
            this.dialog = ProgressDialog.show(this.context, "", "Сохранение. Пожалуйста подождите...", true);
        }
        String serverUrl = ServerInfo.getInstance().getServerUrl();
        HttpTextDataRequest request = new HttpTextDataRequest(this.context);

        request.addResponseListener(new IDataTextResponse() {
            @Override
            public void onTextResponse(String text) {
                if (dialogFlag) {
                    dialog.hide();
                }
            }

            @Override
            public void onError() {
                if (dialogFlag) {
                    dialog.hide();
                }
            }
        });

        request.addResponseListener(iDataTextResponse);

        request.setURL(serverUrl + "/mobile/saveBill");
        request.addData(jBill.toString());
        request.send(this.context);
    }


    public void getOffer(IDataTextResponse iDataTextResponse, String barcode) {
        final boolean dialogFlag = (this.dialog != null && !this.dialog.isShowing());
        if (dialogFlag) {
            this.dialog = ProgressDialog.show(this.context, "", "Загрузка. Пожалуйста подождите...", true);
        }
        String serverUrl = ServerInfo.getInstance().getServerUrl();
        HttpTextDataRequest request = new HttpTextDataRequest(this.context);

        request.addResponseListener(new IDataTextResponse() {
            @Override
            public void onTextResponse(String text) {
                if (dialogFlag) {
                    dialog.hide();
                }
            }

            @Override
            public void onError() {
                if (dialogFlag) {
                    dialog.hide();
                }
            }
        });

        request.addResponseListener(iDataTextResponse);

        request.setURL(serverUrl + "/mobile/getOffer");
        request.addData("barcode", barcode);
        request.send(this.context);
    }

    public void getOffers(IDataTextResponse iDataTextResponse, String partName) {
        final boolean dialogFlag = (this.dialog != null && !this.dialog.isShowing());
        if (dialogFlag) {
            this.dialog = ProgressDialog.show(this.context, "", "Загрузка. Пожалуйста подождите...", true);
        }
        String serverUrl = ServerInfo.getInstance().getServerUrl();
        HttpTextDataRequest request = new HttpTextDataRequest(this.context);

        request.addResponseListener(new IDataTextResponse() {
            @Override
            public void onTextResponse(String text) {
                if (dialogFlag) {
                    dialog.hide();
                }
            }

            @Override
            public void onError() {
                if (dialogFlag) {
                    dialog.hide();
                }
            }
        });

        request.addResponseListener(iDataTextResponse);

        request.setURL(serverUrl + "/mobile/getOffers");
        request.addData("partName", partName);
        request.send(this.context);
    }

    public void saveOffer(Offer offer, IDataTextResponse iDataTextResponse) {
        final boolean dialogFlag = (this.dialog != null && !this.dialog.isShowing());
        if (dialogFlag) {
            this.dialog = ProgressDialog.show(this.context, "", "Сохранение. Пожалуйста подождите...", true);
        }
        String serverUrl = ServerInfo.getInstance().getServerUrl();
        HttpTextDataRequest request = new HttpTextDataRequest(this.context);

        request.addResponseListener(new IDataTextResponse() {
            @Override
            public void onTextResponse(String text) {
                if (dialogFlag) {
                    dialog.hide();
                }
            }

            @Override
            public void onError() {
                if (dialogFlag) {
                    dialog.hide();
                }
            }
        });

        request.addResponseListener(iDataTextResponse);

        request.setURL(serverUrl + "/mobile/saveOffer");
        request.addData("data", offer.printJSON());
        request.send(this.context);
    }

}
